package com.plusapps.inkwang;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.plusapps.inkwang.adapter.ColumnListAdapter;
import com.plusapps.inkwang.model.RSSItem;

public class ColumnListActivity extends Activity {
	private ArrayList<RSSItem> columnList;
	
	private ListView listView;
	
	private ProgressDialog progressDialog;
	private final int PROGRESS_DIALOG = 0;
	
    protected Activity parentContext = this;

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.column_list);

        new backgroundLoadListView().execute();
    }
    
    @Override
    protected Dialog onCreateDialog (int id) {
    	switch (id) {
    	case (PROGRESS_DIALOG):
    		progressDialog = new ProgressDialog(this);
    		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    		progressDialog.setMessage("Now loading..");
    		return progressDialog;
    	}
    	return null;
    }
    
    
    protected ArrayList<RSSItem> makeColumnList () {
    	ArrayList<RSSItem> list = new ArrayList<RSSItem>();

    	try {
    	
	    	URL url = new URL("http://new.inkwang.net/?feed=rss2&cat=7");
	    	HttpURLConnection conn = (HttpURLConnection) url.openConnection();
	    	InputStream is = conn.getInputStream();
	    	
	    	SAXBuilder builder = new SAXBuilder();
	    	Document doc = builder.build(is);
	    	Element channelElement = doc.getRootElement().getChild("channel");
	
	    	for (Object o : channelElement.getChildren("item")) {
	    		Element ele = (Element)o;
	    		RSSItem item = new RSSItem();
	    		
	    		
	    		item.setTitle( ele.getChild("title").getValue() );
	    		item.setLink( ele.getChild("link").getValue() );
	    		item.setPubDate( ele.getChild("pubDate").getValue() );
	    		item.setDescription( ele.getChild("description").getValue() );
	    		item.setContent( ele.getChild("encoded", ele.getNamespace("content")).getValue() );
	    		
	    		list.add(item);
	    	}

	    	
    	} catch (Exception e) {
    		Log.e("EEEEEEEEEEEEEEEEEEEEEEEEE", e.toString());
    	}
    	
    	return list;
    }

    
    
    private class backgroundLoadListView extends AsyncTask<Void, Void, Void> {
    	
    	@Override
    	protected void onPostExecute (Void result) {
			ColumnListAdapter adapter = new ColumnListAdapter(parentContext, R.layout.column_list_item_1, columnList);
            listView = (ListView) findViewById(R.id.column_list_view);
            listView.setAdapter(adapter);

            listView.setOnItemClickListener(new OnItemClickListener () {
    			@Override
    			public void onItemClick(AdapterView<?> av, View v, int position, long id) {
    				RSSItem column = (RSSItem) av.getItemAtPosition(position);
    				
    				Intent intent = new Intent(parentContext, ColumnDetailActivity.class);
    				intent.putExtra("column", column);

    				v.getContext().startActivity(intent);
    			}
            });
    		
            progressDialog.dismiss();
            parentContext.removeDialog(PROGRESS_DIALOG);
    	}
    	
    	@Override
    	protected void onPreExecute () {
    		parentContext.showDialog(PROGRESS_DIALOG);
    	}

		@Override
		protected Void doInBackground(Void... params) {
	        columnList = makeColumnList();

			return null;
		}
    	
    }

}
